<template>
	<view class="login-content">
		<view class="login">
			<view class="login-item login_phone">
				手机号登录
			</view>
			<view class="login-item login_wx" @click="loginWx">
				微信账号登录 
			</view>
			<view class="login-item login_vcode" @click="loginVcode"> 短信验证码登录 </view>
		</view>
		
		<view class="login-footer">
			登录即代表您同意我们的服务协议和隐私政策
			<view>
				添加邀请码
			</view>
		</view>
		
		<uni-popup ref="popup" type="bottom">
			<view class="pop-vcode">
				<view class="pop-title">
					短信验证码登录
					<view>
						{{msg}}
					</view>
				</view>
				<view>
					<view class="pop-vcode-item">
						<input type="text" placeholder="注册或绑定手机号">
					</view>
					<view class="pop-vcode-item">
						<input type="text" placeholder="动态码">
					</view>
				</view>
				<view class="vcode-login">登 录</view>
			</view>
		</uni-popup>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msg:'',//请输入手机号和动态码
			}
		},
		onLoad() {
			if (uni.getStorageSync('uni_id_token_expired') > Date.now()) {
				// uni.navigateTo({
				// 	url: '/pages/goods/goods'
				// })
			}
		},
		methods: {
			onGetPhoneNumber(e){  
				
			} ,
			loginWx(){
				uni.showLoading({
					title: '登录中...',
					mask: true
				})
				new Promise((resolve, reject)=>{
					uni.getProvider({
						service: 'oauth',
						success(res) {
							resolve(res.provider)
						},
						fail(err) {
							reject(new Error(err.errMsg))
						}
					})
				}).then((provider)=>{
					return new Promise((resolve, reject) => {
						uni.login({
							provider,
							success: (res) => {
								console.log('res',res)
								resolve({
									code: res.code,
									provider
								})
							},
							fail: (err) => {
								reject(new Error(err.errMsg))
							}
						})
					})
				}).then(({
					code,
					provider
				}) => {
					return uniCloud.callFunction({
						name: 'user-center',
						data: {
							action: provider.indexOf('weixin') > -1  ? 'loginByWeixin' : 'loginByAlipay',
							params: {
								code
							}
						}
					})
				}).then((res) => {
					console.log(res);
					// if (res.result.code === 0) {
					// 	uni.hideLoading()
					// 	// 如果使用2.7.15及以上版本建议存为uni_id_token
					// 	uni.setStorageSync('uniIdToken', res.result.token)
					// 	uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)
					// 	uni.navigateTo({
					// 		url: '/pages/goods/goods'
					// 	})
					// } else {
					// 	throw new Error(res.result.msg)
					// }
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: err.message || '登录失败',
						showCancel: false
					})
				})
				
				
				// uni.login({
				//   provider: 'weixin',
				//   success: function (loginRes) {
				//     console.log(loginRes.authResult);
				//     // 获取用户信息
				//     uni.getUserInfo({
				//       provider: 'weixin',
				//       success: function (infoRes) {
				//         console.log('用户昵称为：' + infoRes.userInfo.nickName);
				//       }
				//     });
				//   }
				// });
			},
			loginVcode(){
				this.$refs.popup.open()
			},
			login() {},
		}
	}
</script>

<style>
	.login-content{
		height: calc(100vh - 44px);
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		box-sizing: border-box;
	}
	.login{
		padding: 20rpx;
		text-align: center;
		box-sizing: border-box;
	}
	.login-item{
		margin: 20rpx;
		border-radius: 10rpx;
		padding: 20rpx;
		font-size: 1rem;
		color: #fff;
		border: 1px solid #ccc;
	}
	.login_phone{
		background: #007AFF;
	}
	.login_wx{
		background-color: #09BB07;
	}
	.login_vcode{
		color: #949494;
	}
	.login-footer{
		text-align: center;
		padding-bottom: 20rpx;
	}
	.pop-vcode{
		background-color: #fff;
		padding: 20rpx;
	}
	.pop-title{
		text-align: center;
	}
	.pop-vcode-item{
		margin: 20rpx;
		background-color: #d8d8d8;
	}
	.pop-vcode-item input{
		padding: 20rpx;
	}
	.vcode-login{
		color: #fff;
		background-color: #DF5000;
		border-radius: 10rpx;
		padding: 10rpx;
		text-align: center;
	}
</style>
